﻿using Aptech.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemBLL;
using SystemModels;

namespace SystemUI
{
        /// <summary>
        /// 程序主窗体
        /// </summary>
        public partial class frmMain : Form
        {
            /// <summary>
            /// BDQN 主窗体
            /// </summary>
            public frmMain()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 用户
            /// </summary>
            public Users _user = null;
            //所有菜单项集合
            List<Programs> _menus;
            //主菜单集合
            List<ToolStripMenuItem> item;
            //sideBar是否显示
            bool isShow = true;
            //splitter1的初始宽度
            int _oldWidth = 0;
            /// <summary>
            /// 初始化Users对象
            /// </summary>
            /// <param name="user"></param>
            public frmMain(Users user)
                : this()
            {
                _user = user;
            }

            /// <summary>
            /// 窗体加载事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmMain_Load(object sender, EventArgs e)
            {
                //初始化加载splitter的宽度
                _oldWidth = this.sideBar1.Width + 5;
                //初始化单击图片水平距离
                this.pictureBox1.Left = this.sideBar1.Width;
                if (_user != null)
                {
                    //动态加载Label控件，显示用户的名称
                    LabelUser(_user.UserName);
                    _menus = new MenuBLL().QueryMenusAll(_user.UserId);
                }
                else
                {
                    _menus = new MenuBLL().QueryMenusAll();
                }
                //添加菜单
                menus_Show();
                //加载事件
                timer1_Tick(null, null);
            }

            #region 动态显示用户的姓名
            /// <summary>
            /// 动态创建Label控件,显示用户的姓名
            /// </summary>
            private void LabelUser(string txt)
            {
                Label lblUserName = new Label();
                lblUserName.AutoSize = true;
                lblUserName.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(223)))), ((int)(((byte)(235)))), ((int)(((byte)(246)))));
                lblUserName.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                lblUserName.ForeColor = System.Drawing.Color.Red;
                lblUserName.Location = new System.Drawing.Point(6, 8);
                lblUserName.Name = "lblUserName";
                lblUserName.Size = new System.Drawing.Size(100, 12);
                lblUserName.Text = txt;
                this.panel1.Controls.Add(lblUserName);
                this.label1.Location = new System.Drawing.Point(6 + this.panel1.Controls["lblUserName"].Size.Width, 8);

            }
            #endregion

            #region 动态添加菜单项
            /// <summary>
            ///动态添加菜单项 
            /// </summary>
            private void menus_Show()
            {
                //主菜单集合
                item = new List<ToolStripMenuItem>();
                //添加ToolBar、SideBar组、MenuStrip主菜单项
                foreach (Programs menu in _menus)
                {
                    //添加主菜单
                    if (menu.ParentId == 0)
                    {
                        //保存到主菜单集合
                        item.Add(AddMenu(menu, null));
                        //为SideBar添加到组
                        SbGroup group = new SbGroup(menu.ProgramName);
                        this.sideBar1.AddGroup(group);
                    }
                    //添加到ToolBar
                    if (menu.ShowToolBar == 1) AddToolBar(menu);
                }
                //添加子菜单
                AddSubItem();
            }
            #endregion

            #region 添加子菜单、组的工具栏项
            /// <summary>
            ///添加子菜单、组的工具栏项
            /// </summary>
            private void AddSubItem()
            {
                foreach (Programs sItem in _menus)
                {
                    for (int i = 0; i < item.Count; i++)
                    {
                        /*方便之后通过此Tag来获取每个父级控件的ProgramId来绑定每一个子控件的sItem.ParentId。
                                   * （每一个子控件都有一个ParentId与父控件的ProgramId相对应。）*/
                        if (sItem.ParentId == ((Programs)item[i].Tag).ProgramId)
                        {
                            AddMenu(sItem, item[i]);
                            //添加到SideBar的项
                            this.imageList1.Images.Add(Image.FromFile(sItem.ImagePath));
                            SbItem sbitem = new SbItem(sItem.ProgramName, this.imageList1.Images.Count - 1);
                            sbitem.Tag = sItem;
                            this.sideBar1.Groups[i].Items.Add(sbitem);
                            break;
                        }
                    }
                }
                this.sideBar1.ItemClick += new SbItemEventHandler(sideBar1_ItemClick);
            }
            #endregion

            #region 添加工具栏
            /// <summary>
            /// 添加工具栏
            /// </summary>
            /// <param name="menu">工具项</param>
            private void AddToolBar(Programs menu)
            {
                ToolStripButton btn = new ToolStripButton();
                //设置tool工具栏的文本
                btn.Text = menu.ProgramName;
                //设置tool工具栏的图片
                btn.Image = Image.FromFile(menu.ImagePath);
                //设置ToolBar的显示方式
                btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
                btn.Tag = menu;
                this.toolStrip1.Items.Add(btn);
                btn.Click += new EventHandler(btn_Click);
            }
            #endregion

            #region 添加菜单项
            /// <summary>
            /// 添加菜单项
            /// </summary>
            /// <param name="menu">菜单项</param>
            /// <param name="item">父级菜单项</param>
            /// <returns></returns>
            private ToolStripMenuItem AddMenu(Programs menu, ToolStripMenuItem item)
            {
                ToolStripMenuItem mimenu = new ToolStripMenuItem(menu.ProgramName, Image.FromFile(menu.ImagePath));
                mimenu.Tag = menu;/*在获取每一个父级控件的同时，捕获当前父级标签本身的这个类存储在父级控件的Tag身上，
                                   * 方便之后通过此Tag来获取每个父级控件的ProgramId来绑定每一个子控件的sItem.ParentId。
                                   * （每一个子控件都有一个ParentId与父控件的ProgramId相对应。）*/
                //添加子项
                if (item != null)
                {
                    item.DropDownItems.Add(mimenu);
                    mimenu.Click += new EventHandler(mimenu_Click);
                }
                else//添加菜单主项
                {
                    this.menuStrip1.Items.Add(mimenu);
                }
                return mimenu;
            }
            #endregion

            #region 添加菜单的点击事件
            /// <summary>
            /// sideBar添加单击事件
            /// </summary>
            /// <param name="e"></param>
            void sideBar1_ItemClick(SbItemEventArgs e)
            {
                AddMenuClick(e.Item.Tag);
            }
            /// <summary>
            /// 工具栏单击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void btn_Click(object sender, EventArgs e)
            {
                AddMenuClick(((ToolStripButton)sender).Tag);
            }
            /// <summary>
            /// 菜单单击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void mimenu_Click(object sender, EventArgs e)
            {
                AddMenuClick(((ToolStripItem)sender).Tag);
            }

            /// <summary>
            /// 添加菜单的点击事件
            /// </summary>
            /// <param name="sender"></param>
            private void AddMenuClick(object sender)
            {
                Programs menu = (Programs)sender;
                //添加状态栏显示项
                AddStatusMenus(menu);
                //判断是否打开窗口
                if (menu != null && menu.ProgramPath.Length != 0) OpenWindows(menu.ProgramPath);
            }
            #endregion

            #region  添加状态栏的显示菜单
            /// <summary>
            /// 添加状态栏的显示菜单
            /// </summary>
            /// <param name="menu"></param>
            private void AddStatusMenus(Programs menu)
            {
                this.toolStripStatusLabel5.Text = menu.ProgramName;
                for (int i = 0; i < item.Count; i++)
                {
                    if (menu.ParentId == ((Programs)item[i].Tag).ProgramId)
                    {
                        //添加状态栏对应的菜单项(系统底部菜单项)
                        this.toolStripStatusLabel4.Text = ((Programs)item[i].Tag).ProgramName;
                        break;
                    }
                }
            }
            #endregion

            #region 打开当要显示的窗口
            /// <summary>
            /// 显示要打开的窗体
            /// </summary>
            /// <param name="window">要打开的窗体名称</param>
            void OpenWindows(string window)
            {
                /*Form obj = null;
                switch (window)
                {
                    case "frmGetRights":
                        obj = new frmGetRights();
                        break;
                    case "frmDeleteUser":
                        obj = new frmDeleteUser();
                        break;
                    case "frmAddUser":
                        obj = new frmAddUser();
                        break;
                    case "frmGetRoles":
                        obj = new frmGetRoles();
                        break;
                    case "frmChangePwd":
                        obj = new frmChangePwd(this._user);
                        break;
                    case "frmUserInfo":
                        obj = new frmUserInfo(this._user);
                        break;
                    case "frmQuestionQuery":
                        obj = new frmQuestionQuery();
                        break;
                    case "frmAllQuestions":
                        obj = new frmAllQuestions();
                        break;
                    case "frmSingleChoice":
                        obj = new frmSingleChoice();
                        break;
                    case "frmjudge":
                        obj = new frmjudge();
                        break;
                    case "frmQuestion":
                        obj = new frmQuestion();
                        break;
                    case "frmTestSearch":
                        obj = new frmTestSearch();
                        break;
                    case "frmListQuestions":
                        obj = new frmListQuestions();
                        break;
                    case "frmSharpQuestions":
                        obj = new frmSharpQuestions();
                        break;
                    case "退出":
                        Application.Exit();
                        break;
                    default:
                        MessageBox.Show("该功能未实现！");
                        break;
                }
                if (obj != null)
                {
                    obj.MdiParent = this;
                    this.plBackground.Visible = false;
                    for (int i = 0; i < this.MdiChildren.Length - 1; i++)
                    {
                        this.MdiChildren[i].Close();
                    }
                    obj.Dock = DockStyle.Fill;
                    obj.Show();
                }*/
            }


            /// <summary>
            /// 子窗体填充显示在父窗体中
            /// </summary>
            private void MdiChrildrenFillShow()
            {
                foreach (Form item in this.MdiChildren)
                {
                    item.Dock = DockStyle.Fill;
                }
            }
            #endregion

            #region sideBar隐藏与实现
            /// <summary>
            /// sideBar隐藏与实现
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void pictureBox1_Click(object sender, EventArgs e)
            {
                if (isShow)
                {
                    this.splitter1.Width = 5;
                    this.pictureBox1.Image = SystemUI.Properties.Resources.flexLeft;
                    isShow = false;
                }
                else
                {
                    this.splitter1.Width = _oldWidth;
                    this.pictureBox1.Image = SystemUI.Properties.Resources.flexRight;
                    isShow = true;
                }
                this.sideBar1.Width = this.splitter1.Width - 5;
                this.pictureBox1.Left = this.sideBar1.Width;
            }

            #endregion

            #region 加载状态栏显示时间
            /// <summary>
            /// 加载状态栏显示时间
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void timer1_Tick(object sender, EventArgs e)
            {
                DateTime dt = DateTime.Now;
                this.toolStripStatusLabel6.Text = dt.ToString("yyyy年MM月dd日 hh:mm:ss");
            }
            #endregion

            #region 窗体关闭时程序退出
            /// <summary>
            /// 程序退出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
            {
                this._user.UserStatus = 0;
                if (new UserBLL().ChangeUser(_user))
                    Application.Exit();
            }
            #endregion

            #region 当窗体面积改变时，子窗体面积同步改变
            /// <summary>
            /// 当窗体面积改变时，子窗体面积同步改变
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmMain_SizeChanged(object sender, EventArgs e)
            {
                this.pictureBox1.Top = this.sideBar1.Top + (this.sideBar1.Height / 2 - this.pictureBox1.Height / 2);
                MdiChrildrenFillShow();
            }
            #endregion

            #region 实现splitter的拖动,子窗体的面积改变
            /// <summary>
            /// 实现splitter的拖动
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void splitter1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    this.splitter1.Width = e.X;
                    this.sideBar1.Width = e.X - 5;
                    this.pictureBox1.Left = this.sideBar1.Width;
                }
            }

            /// <summary>
            /// 实现splitter/sideBar/pictureBox收缩
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void splitter1_SizeChanged(object sender, EventArgs e)
            {
                this.pictureBox1.Left = this.sideBar1.Width;
            }
            #endregion


        }
    
}
